package com.leemz.design.chapter5.pattern;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * appConfig的单例实现
 */
public class SingletonAppConfig {

    private static SingletonAppConfig singleton = new SingletonAppConfig();

    private String parameterA;
    private String parameterB;

    public static SingletonAppConfig getInstance() {
        return singleton;
    }

    public String getParameterA() {
        return parameterA;
    }

    public String getParameterB() {
        return parameterB;
    }

    private SingletonAppConfig() {
        readConfig();
    }

    private void readConfig() {
        Properties p = new Properties();
        InputStream is = null;
        try {
            is = SingletonAppConfig.class.getClassLoader().getResourceAsStream("app_config.properties");
            p.load(is);
            parameterA = p.getProperty("paramA");
            parameterB = p.getProperty("paramB");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
